package com.vay.de.dang.online.view;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class VnItemLayout extends FrameLayout {

    static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    private TextView necessaryTv;
    private TextView labelTv;
    private TextView warringTv;
    private EditText inputEd;
    private ImageView dropIcon;
    private TextView contractTv;

    private String label;
    private boolean editMode;

    // 最小，最大长度
    private int minLength;
    private int maxLength;

    // 浮点数长度
    private int floatF1;
    private int floatF2;

    private OnItemClick onItemClick;

    public VnItemLayout(Context context) {
        this(context, null);
    }

    public VnItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.group_item_layout, null);
        addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        necessaryTv = findViewById(R.id.necessary_tv);
        labelTv = findViewById(R.id.label_tv);
        warringTv = findViewById(R.id.warring_tv);
        inputEd = findViewById(R.id.content_ed);
        warringTv = findViewById(R.id.warring_tv);
        dropIcon = findViewById(R.id.bnt_selector);
        contractTv = findViewById(R.id.hint_select_contract);

        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VnEditContent);
            boolean edit = ta.getBoolean(R.styleable.VnEditContent_edit, true);
            boolean required = ta.getBoolean(R.styleable.VnEditContent_required, false);
            label = ta.getString(R.styleable.VnEditContent_label);
            String inputType = ta.getString(R.styleable.VnEditContent_type);

            String hint = ta.getString(R.styleable.VnEditContent_hint);
            // 最小，最大
            minLength = ta.getInt(R.styleable.VnEditContent_min, 0);
            maxLength = ta.getInt(R.styleable.VnEditContent_max, 100);

            // 浮点数
            floatF1 = ta.getInt(R.styleable.VnEditContent_floatL1, 0);
            floatF2 = ta.getInt(R.styleable.VnEditContent_floatL2, 0);

            labelTv.setText(label);
            inputEd.setHint(hint);

            setRequired(required);
            setInputType(inputType);
            setEditEnable(edit);

            ta.recycle();
        }

        necessaryTv = findViewById(R.id.necessary_tv);
        labelTv = findViewById(R.id.label_tv);
        warringTv = findViewById(R.id.warring_tv);
        inputEd = findViewById(R.id.content_ed);
    }

    public void setRequired(boolean required) {
        necessaryTv.setVisibility(required ? View.VISIBLE : View.GONE);
    }

    public void setLengthFilter(int length) {
        inputEd.setFilters(new InputFilter[]{new VnSizeFilter(length)});
    }

    public void setLabel(String label) {
        labelTv.setText(label);
    }

    private void setInputType(String inputType) {
        // string
        if ("0".equals(inputType)) {
            inputEd.setFocusable(true);
            dropIcon.setVisibility(View.GONE);
            if (maxLength > 0) {
                inputEd.setFilters(new InputFilter[]{new VnSizeFilter(maxLength)});
            }
        }
        // number
        else if ("1".equals(inputType)) {
            dropIcon.setVisibility(View.GONE);
            inputEd.setFocusable(true);
            inputEd.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
            inputEd.setFilters(new InputFilter[]{new VnSizeFilter(maxLength)});
        }
        // float
        else if ("2".equals(inputType)) {

        }
        // phone
        else if ("3".equals(inputType)) {
            dropIcon.setVisibility(View.GONE);
            inputEd.setFocusable(true);
            inputEd.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
            inputEd.setFilters(new InputFilter[]{new VnSizeFilter(maxLength)});
        }
        // english
        else if ("4".equals(inputType)) {

        }
        // none
        else if ("5".equals(inputType)) {
            inputEd.setFocusable(false);
            OnClickListener click = view -> {
                if (onItemClick != null) {
                    onItemClick.onClick();
                }
            };
            inputEd.setOnClickListener(click);
            inputEd.setBackgroundResource(R.drawable.bg_input_none);
        }
        // list
        else if ("6".equals(inputType)) {

        }
        // date
        else if ("7".equals(inputType)) {
            inputEd.setFocusable(false);
            OnClickListener click = view -> {
                //showDate();
                showDate2();
            };
            dropIcon.setVisibility(View.VISIBLE);
            inputEd.setOnClickListener(click);
        }
        // query
        else if ("8".equals(inputType)) {
            inputEd.setFocusable(false);
            OnClickListener click = view -> {
                if (onItemClick != null) {
                    onItemClick.onClick();
                }
            };
            dropIcon.setVisibility(View.VISIBLE);
            inputEd.setOnClickListener(click);
        }
        // contract
        else if ("9".equals(inputType)) {
            inputEd.setFocusable(false);
            OnClickListener click = view -> {
                if (onItemClick != null) {
                    onItemClick.onClick();
                }
            };
            inputEd.setOnClickListener(click);
            inputEd.setBackgroundResource(R.drawable.bg_input_none);
            contractTv.setVisibility(View.VISIBLE);
        }
    }

    private void showDate() {
        DatePickerDialog.OnDateSetListener listener = (datePicker, year, month, day) -> {
            setContent(year + "-" + VnUtil.toTwoStr(month + 1) + "-" + VnUtil.toTwoStr(day));
        };
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(getContext(), listener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MARCH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    public void showDate2() {
        String cancel = getContext().getString(R.string.bnt_cancel);
        String confirm = getContext().getString(R.string.bnt_confirm);

        Calendar start = Calendar.getInstance();
        Calendar cur = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        try {
            start.setTime(df.parse("1940-01-01"));
            end.setTime(new Date());
            end.add(Calendar.YEAR, -18);

            cur.setTime(df.parse("2000-01-01"));
        } catch (Exception e) {
        }

        TimePickerView pvTime = new TimePickerBuilder(getContext(), (date, v) -> {
            setContent(df.format(date));
        })
                .setSubmitText(confirm)
                .setCancelText(cancel)
                .setCancelColor(Color.parseColor("#999999"))
                .setSubmitColor(Color.parseColor("#43AE63"))
                .setSubCalSize(14)
                .setTitleText(label)
                .setTitleSize(16)
                .setContentTextSize(16)
                .setRangDate(start, end)
                .setTitleColor(Color.parseColor("#43AE63"))
                .setLineSpacingMultiplier(2.0f)
                .setBgColor(Color.WHITE)
                .setTitleBgColor(Color.parseColor("#f8f8f8"))
                .isAlphaGradient(true)
                .setType(new boolean[]{true, true, true, false, false, false}) //显示“年月日时分秒”的哪几项
                .setLabel("", "", "", "", "", "")
                .isCenterLabel(false) //是否只显示选中的label文字，false则每项item全部都带有 label
                .build();
        pvTime.setDate(cur);
        pvTime.show();
    }

    public void setEditEnable(boolean enable) {
        inputEd.setEnabled(editMode = enable);
    }

    public void setEnableDrop(boolean enable) {
        dropIcon.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
    }

    public void setOnItemClick(OnItemClick click) {
        onItemClick = click;
    }

    public void setContent(String content) {
        inputEd.setText(content);
    }

    public String getContent() {
        return inputEd.getText().toString();
    }

    public void showWarring(String text) {
        if (VnUtil.isEmpty(text)) {
            warringTv.setVisibility(View.GONE);
        } else {
            warringTv.setVisibility(View.VISIBLE);
            warringTv.setText(text);
        }
    }

    public interface OnItemClick {

        void onClick();
    }
}
